package pref.junx.flybird.activity.addevent;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import pref.junx.flybird.R;
import pref.junx.flybird.activity.main.NewMainActivity;
import pref.junx.flybird.base.BaseActicvitylhy;
import pref.junx.flybird.entity.Event;
import pref.junx.flybird.tools.JumpUtil;
import pref.junx.flybird.tools.TimeChange;
import pref.junx.flybird.view.CustomDatePicker;


/**
 * Created by 半生瓜 on 2017/8/7.
 */

public class AddEventActvity extends BaseActicvitylhy<AddEventPresenter, AddEventModel> implements AddEventConteact.View {
    private String add_time;
    private TextView currentTime;
    private CustomDatePicker customDatePicker2;
    TextView finish_send_msg,ok_send_msg;
    EditText ET_add_title,ET_add_ZONE,ET_add_value;
    @Override
    public int getLayoutId() {
return R.layout.send_msg_incloud;    }

    @Override
    public void initview() {
        currentTime = (TextView) findViewById(R.id.time1);
        currentTime.setOnClickListener(this);
        finish_send_msg=(TextView)findViewById(R.id.finish_send_msg);
        finish_send_msg.setOnClickListener(this);
        ok_send_msg=(TextView)findViewById(R.id.ok_send_msg);
        ok_send_msg.setOnClickListener(this);
         ET_add_title=(EditText) findViewById(R.id.ET_add_title);
        ET_add_ZONE=(EditText)findViewById(R.id.ET_add_ZONE);
         ET_add_value=(EditText)findViewById(R.id.ET_add_value);


    }

    @Override
    public void processClick(View view) {
        switch (view.getId()){
            case R.id.time1:
                customDatePicker2.show(currentTime.getText().toString());break;
            case R.id.finish_send_msg:
                finish();break;
            case R.id.ok_send_msg:
                if(ET_add_title.getText().toString().length()==0&&ET_add_ZONE.getText().toString().length()==0&&ET_add_value.getText().toString().length()==0){
                    t.show_l("请输入完整信息");
                }else {
                    Event event=new Event();
                    event.setEvent_table(ET_add_title.getText().toString().trim());
                    event.setEvent_zone(ET_add_ZONE.getText().toString().trim());
                    try {
                        event.setEvent_time(Integer.valueOf((int) TimeChange.dateToStamp(currentTime.getText().toString())));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    event.setEvent_user_id(String.valueOf(spTools.getInt("user_id")));
                    event.setEvent_value(ET_add_value.getText().toString().trim());
                    presenter.sengEvent(event);
                }
        }

    }


    @Override
    public void init() {
        initDatePicker();
        customDatePicker2.show(currentTime.getText().toString());    }

    private void initDatePicker() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());

        currentTime.setText(now);
        customDatePicker2 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                currentTime.setText(time);
                add_time = time;
            }
        }, now, "2050-01-01 00:00"); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker2.showSpecificTime(true); // 显示时和分
        customDatePicker2.setIsLoop(false); // 允许循环滚动
    }



    @Override
    public void success() {

    }

    @Override
    public void result(String a) {

    }

    @Override
    public void susses() {
        t.show_l("修改失败");
        NewMainActivity.newMainActivity.finish();
        JumpUtil.to(c,NewMainActivity.class);
        finish();
    }

    @Override
    public void fail() {
        t.show_l("修改失败");
    }
}


